package com.agh.portal.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

import com.agh.portal.domain.enumeration.AdvertisementCategory;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findAdsByStudent", "findAdsByCategory", "findAdsByTitleLike", "findAdsByTitleAndContentLike", "findAdsByCreationDateBetween" })
public class Ad {

    @Size(max = 80)
    @NotEmpty
    private String title;

    @Size(max = 1000)
    @NotEmpty
    private String content;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date creationDate;

    @Enumerated
    private AdvertisementCategory category;

    @ManyToOne
    private Student student;

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
	
    public static List<Post> findAllPosts() {
        return entityManager().createQuery("SELECT o FROM Post o ORDER BY o.postDate desc", Post.class).getResultList();
    }
    
    public static List<Post> findPostEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Post o ORDER BY o.postDate desc", Post.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }
    
    public static TypedQuery<Ad> findLatestAdsByStudent(Student student, Integer maxResults) {
        if (student == null) throw new IllegalArgumentException("The student argument is required");
        EntityManager em = Ad.entityManager();
        TypedQuery<Ad> q = em.createQuery("SELECT o FROM Ad AS o WHERE o.student = :student ORDER BY o.creationDate desc", Ad.class);
        q.setParameter("student", student);
        if(maxResults != null){
        	q.setMaxResults(maxResults);
        }
        return q;
    }
    
    public static TypedQuery<Ad> findLatestAds(Integer maxResults) {
        EntityManager em = Ad.entityManager();
        TypedQuery<Ad> q = em.createQuery("SELECT o FROM Ad AS o ORDER BY o.creationDate desc", Ad.class);
        if(maxResults != null){
        	q.setMaxResults(maxResults);
        }
        return q;
    }
    
    public static TypedQuery<Ad> findAdEntriesByCategory(AdvertisementCategory category, int firstResult, int maxResults, String sortFieldName, String sortOrder) {
        if (category == null) throw new IllegalArgumentException("The category argument is required");
        EntityManager em = Ad.entityManager();
        String jpaQuery = "SELECT o FROM Ad AS o WHERE o.category = :category";
        if (fieldNames4OrderClauseFilter.contains(sortFieldName)) {
            jpaQuery = jpaQuery + " ORDER BY " + sortFieldName;
            if ("ASC".equalsIgnoreCase(sortOrder) || "DESC".equalsIgnoreCase(sortOrder)) {
                jpaQuery = jpaQuery + " " + sortOrder;
            }
        }
        TypedQuery<Ad> q = em.createQuery(jpaQuery, Ad.class).setFirstResult(firstResult).setMaxResults(maxResults);
        q.setParameter("category", category);
        return q;
    }
    
}
